package com.stox.charting.ui.swing.event.listener;

import java.util.List;

import com.stox.charting.ui.swing.event.ChartModelChangedEvent;
import com.stox.charting.ui.swing.model.ChartModel.ChartModelListener;
import com.stox.charting.ui.swing.model.price.PriceChartModel;
import com.stox.core.manager.BarManager;
import com.stox.core.model.IBar;
import com.stox.core.model.intf.HasBars;
import com.stox.ioc.Container;

public class HasBarsChangedEventListener implements ChartModelListener {

	private BarManager barManager;
	
	public HasBarsChangedEventListener() {
		this.barManager = Container.get(BarManager.class);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public void chartModelChanged(final ChartModelChangedEvent event) {
		if(event.getProperty().equalsIgnoreCase("hasBars")){
			PriceChartModel<IBar> defaultChartModel = (PriceChartModel<IBar>)event.getSource();
			HasBars hasBars = defaultChartModel.getHasBars();
			List<IBar> bars = barManager.get(hasBars.getCode(), hasBars.getExchangeCode());
			defaultChartModel.setBackupData(bars);

			defaultChartModel.getChart().getChartView().getChartingInternalFrameView().setTitle(hasBars.getName()+"("+hasBars.getCode()+")");
		}
	}
}
